package com.newcapec.news.servlet;

import com.newcapec.news.entity.News;
import com.newcapec.news.entity.SysUser;
import com.newcapec.news.service.NewService;
import com.newcapec.news.service.SysUserService;
import com.newcapec.news.service.SysUserServiceImpl;
import com.newcapec.news.service.impl.NewServiceImpl;
import com.newcapec.news.util.MD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description: 添加用户信息
 * @Author: fujun
 * @Date : 2022/3/17 16:17
 * @Version : 1.0
 * @company 新开普电子股份有限公司
 */
@WebServlet("/addNews")
public class AddNewsServlet extends HttpServlet {

    private Logger  logger= LoggerFactory.getLogger(AddNewsServlet.class);
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("utf-8");
//        resp.setCharacterEncoding("utf-8");

        // 添加新闻的servlet
        //获取用户传递过来的参数
        String newsTitile = req.getParameter("newsTitile");
        String channelid = req.getParameter("channelid");
        String newsContext = req.getParameter("newsContext");

        //从session中获取 操作人    
        SysUser user = (SysUser) req.getSession().getAttribute("user");

        if(newsTitile!=null && channelid!=null && newsContext!=null ) {

            News news = new News();
            news.setTitle(newsTitile);
            news.setChnl_code(Integer.parseInt(channelid));
            news.setDetail(newsContext);
            news.setOptor(user.getName());

            logger.info("添加了一个新闻信息{}",news.toString());
            NewService newService=new NewServiceImpl();

            newService.insertNews(news);

            resp.sendRedirect("page/NewsManagement.html");

        }else{

            resp.sendRedirect("page/addNews.html");
        }







    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
